package org.nehc.algorithm.code.leetcode;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * TODO
 *
 * @author 陈賝
 * @since 2024/11/25 16:18
 */
public class two_sum_1 {

    public static int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            Set<Integer> integers = map.keySet();
            if (integers.contains(nums[i])) {
                return new int[]{map.get(nums[i]), i};
            }
            Integer complement = target - nums[i];
            map.put(complement, i);
        }
        return null;

        //双指针，二分大法
//        for (int i = 0; i < nums.length; i++) {
//            // 遍历数组，寻找 complement
//            int complement = target - nums[i];
//            // 双指针同时遍历数组，寻找 complement
//            for (int j = i + 1,k = nums.length - 1; j < k; j++, k--) {
//                if (nums[j] == complement){
//                    return new int[]{i, j};
//                }
//                if (nums[k] == complement){
//                    return new int[]{i, k};
//                }
//            }
//        }
//        return new int[]{0};
    }

    public static void main(String[] args) {
        int[] nums = {2, 4, 11, 15, 5};
        int[] ints = twoSum(nums, 9);
        System.out.println(ints[0] + " " + ints[1]);
    }
}
